package img

import(
	"plugin"
	"runtime"
	"fmt"
	// "context"

	// "goauto/model"
)


var pPlugin *plugin.Plugin
var soPath string = "/src/goauto/.dll/resource/img/pkg.dll"

func init() {
	initPlugin()
}

func initPlugin() {
	if pPlugin == nil {
		goroot := runtime.GOROOT()
		soPath := goroot + soPath
		fmt.Println("soPath", soPath)
		pPluginTmp, errTmp := plugin.Open(soPath)
		if errTmp != nil {
			panic(errTmp)
		}
		pPlugin = pPluginTmp
	}
	
	return
}
//获取图片
func GetImg(sid string) (img map[string]any) {
	
	GetImg2, err := pPlugin.Lookup("GetImg")
	if err != nil {
		panic(err)
	}
	img, err = GetImg2.(func(string)(map[string]any, error))(sid)
	if err != nil {
		return
	}
	return
}

//获取图片绝对路径
func GetImgAbsPath(sid string) (absPath string) {
	
	GetImgAbsPath2, err := pPlugin.Lookup("GetImgAbsPath")
	if err != nil {
		panic(err)
	}
	absPath = GetImgAbsPath2.(func(string)(string))(sid)
	return
}

//获取图片宽度
func GetImgWidth(sid string) (width int) {
	
	GetImgWidth2, err := pPlugin.Lookup("GetImgWidth")
	if err != nil {
		panic(err)
	}
	width = GetImgWidth2.(func(string)(int))(sid)
	return
}

//获取图片高度
func GetImgHeight(sid string) (height int) {
	
	GetImgHeight2, err := pPlugin.Lookup("GetImgHeight")
	if err != nil {
		panic(err)
	}
	height = GetImgHeight2.(func(string)(int))(sid)
	return
}